<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Db;
use think\Model as ThinkModel;
use app\common\model\MonthCardItem;
use app\common\model\Project;
use app\common\model\Product;

//银行列表模型
class MonthCardTemplate extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__QT_VIPER_MONTH_CARD_TEMPLATE__';
    protected $name = 'qt_viper_month_card_template';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    /**
     * 插入月卡信息
     * @param $arr
     * @param $store_id
     * @param $viper_type_id
     */
    public static function createByTemplate($arr,$store_id,$viper_type_id)
    {
        foreach ($arr as $key=> $item)
        {
            if(!isset($item['rate'])){
                $item['rate'] = '0';
            }
            $template = self::create([
                'name' => $item['name'],
                'amount' => $item['amount'],
                'rate' => $item['rate'],
                'hz' => $item['hz'],
                'expire' => $item['expire'],
                'type' => $item['type'],
                'store_id' => $store_id,
                'viper_type_id' =>$viper_type_id,
            ]);
            if(!empty($item['item'])){
                foreach ($item['item'] as $k=> $i)
                {
                    if($i != ""){
                        $goods = explode('-',$i);
                        MonthCardItem::create([
                            'item_id' => $goods[1],
                            'item_type' => $goods[0],
                            'store_id' => $store_id,
                            'month_template_id' => $template['id'],
                            'viper_type_id'=>$viper_type_id,
                        ]);
                    }
                }
            }
        }
    }

    public static function getTemplateOfStore($store_id,$viper_type_id)
    {
        $list = self::where(['store_id'=>$store_id,'viper_type_id'=>$viper_type_id])->field('id,name,amount,hz,rate,expire,type')->select();

        foreach ($list as $key=>$item)
        {
            $items = MonthCardItem::where(['month_template_id'=>$item['id']])->field('item_id id,item_type type')->select();
            foreach ($items as $k=>$i)
            {
                $name = '';
                if ($i['type'] == 'goods') {
                    $name = 'goods'.'-'.$i['id'];
                }elseif ($i['type'] == 'project') {
                    $name = 'project'.'-'.$i['id'];
                }
                $items[$k]=$name;
            }
            $item['item'] = $items;
            $list[$key] = $item;
        }

        return $list;
    }


    /**
     * 会员月卡补录
     * @param $arr [提交过来的数据]
     * @param $store_id [门店ID]
     * @param $viper_type_id [会员类型ID]
     * @param $viper_card_id [会员卡ID]
     */
    public static function supplementCreateByTemplate($arr,$userInfo,$viper_type_id,$viper_card_id)
    {
        $arr = json_decode($arr,true);
        foreach ($arr as $key=> $item)
        {
            if(!isset($item['rate'])){
                $item['rate'] = '0';
            }
            $template = self::create([
                'name' => $item['name'],
                'hz' => $item['hz'],
                'expire' => $item['expire'],
                'type' => $item['type'],
                'store_id' => $userInfo['store_id'],
                'viper_type_id' =>$viper_type_id,
                'template_type'=>'2'
            ]);
            if(!empty($item['item'])){
                foreach ($item['item'] as $k=> $i)
                {
                    if($i != ""){
                        $goods = explode('-',$i);
                        MonthCardItem::create([
                            'item_id' => $goods[1],
                            'item_type' => $goods[0],
                            'store_id' => $userInfo['store_id'],
                            'month_template_id' => $template['id'],
                            'viper_type_id'=>$viper_type_id,
                            'template_type'=>'2'
                        ]);
                    }
                }
            }
            //写充值记录
            $card_item = [
                'viper_card_id' => $viper_card_id,
                'viper_type_id' => $viper_type_id,
                'month_template_id' => $template['id'],
                'status' => 1,
            ];
            Db::name('qt_viper_card_item')->insert($card_item);
            $supplement = [
                'viper_card_id' => $viper_card_id,
                'viper_type_id' => $viper_type_id,
                'template_id' => $template['id'],
                'business_type' => 2,
                'creater' => $userInfo['id'],
                'create_time' => time(),
            ];
            Db::name('qt_viper_card_number_supplement')->insert($supplement);

        }
    }


    /**
     * 修改月卡信息
     * @param $arr
     * @param $store_id
     * @param $viper_type_id
     */
    public static function updateByTemplate($arr,$store_id,$viper_type_id)
    {
        foreach ($arr as $key=> $item)
        {
            if(!isset($item['rate'])){
                $item['rate'] = '0';
            }
            if(isset($item['id']) && !empty($item['id'])){
                self::where(['id'=>$item['id']])->update([
                    'name' => $item['name'],
                    'amount' => $item['amount'],
                    'rate' => $item['rate'],
                    'hz' => $item['hz'],
                    'expire' => $item['expire'],
                    'type' => $item['type'],
                    'store_id' => $store_id,
                    'viper_type_id' =>$viper_type_id,
                ]);
                $templateId = $item['id'];
            }else{
                $template = self::create([
                    'name' => $item['name'],
                    'amount' => $item['amount'],
                    'rate' => $item['rate'],
                    'hz' => $item['hz'],
                    'expire' => $item['expire'],
                    'type' => $item['type'],
                    'store_id' => $store_id,
                    'viper_type_id' =>$viper_type_id,
                ]);
                $templateId = $template['id'];
            }

            if(!empty($item['item'])){
                Db::name('qt_viper_month_card_template_item')->where(['month_template_id'=>$templateId])->delete();
                foreach ($item['item'] as $k=> $i)
                {
                    if($i != ""){
                        $goods = explode('-',$i);
                        MonthCardItem::create([
                            'item_id' => $goods[1],
                            'item_type' => $goods[0],
                            'store_id' => $store_id,
                            'month_template_id' => $templateId,
                            'viper_type_id'=>$viper_type_id,
                        ]);
                    }
                }
            }
        }
    }






}